Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

python - scrape the movie details using BS4

Scrape "http://fresco-movies.surge.sh/" and get the movie details and append the data.csv

I have to scrape the data ie movie name, duration, genre, rating, description, Director and votes from the website and save it to data.csv.

Please help me with the code

 from bs4 import BeautifulSoup
import requests
url = "http://fresco-movies.surge.sh/"
req = requests.get(url)
soup = BeautifulSoup(req.content, 'html.parser')
print(soup)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's straight forward navigation of the HTML document

from bs4 import BeautifulSoup
import requests
url = "http://fresco-movies.surge.sh/"
req = requests.get(url)
soup = BeautifulSoup(req.content, 'html.parser')
names = []
for m in soup.find_all("div", class_="row"):
    names.append({"name":m.find("a").text,
                 "director":m.find("div", class_="ratings-bar").find("a").text,
                 "votes":m.find("div", class_="ratings-bar").find("p", class_="sort-num_votes-visible").find_all("span")[1].text,
                 "certificate":m.find("span", class_="certificate"),
                 "runtime":m.find("span", class_="runtime"),

                 })
    
print(pd.DataFrame(names).head(5).to_string(index=False))

output

                                          name              director    votes certificate    runtime
                      The Shawshank Redemption        Frank Darabont  2033239       [9.3]  [142 min]
                                 The Godfather  Francis Ford Coppola  1394179       [9.2]  [175 min]
                               The Dark Knight     Christopher Nolan  2001026       [9.0]  [152 min]
                        The Godfather: Part II  Francis Ford Coppola   966187       [9.0]  [202 min]
 The Lord of the Rings: The Return of the King         Peter Jackson  1447736       [8.9]  [201 min]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...