【发布时间】:2017-08-10 05:05:50
【问题描述】:
在这方面有一些很棒的主题(其中一些帮助我达到了这一点),但我似乎无法弄清楚为什么我的程序不工作。
问题:该程序可以运行,但它似乎只在应该循环遍历所有表行时才返回第一行。
我正在使用 Python 3.5
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = "http://www.the-numbers.com/movies/year/2006"
r = requests.get(url)
soup = BeautifulSoup(r.content)
data = []
for table_row in soup.select("table"):
cells = table_row.find_all(['td'])
release_date = cells[0].text.strip()
movie_name = cells[2].text.strip()
genre_name = cells[3].text.strip()
production_budget = cells[4].text.strip()
box_office = cells[5].text.strip()
movie = {"Release_Date" : release_date,
"Movie_Name" : movie_name,
"Genre" : genre_name,
"Production_Budget" : production_budget,
"Box_Office" : box_office}
data.append(movie)
print (release_date, movie_name, genre_name, production_budget, box_office)
这将返回 2006 年 1 月 BloodRayne Action $25,000,000 $2,405,420,这是正确的,但我需要表中的所有其他行。
如果这个问题很容易解决,下一步就是将其放入 Pandas DataFrame 中(但在响应中不是必需的)。
任何帮助将不胜感激。
【问题讨论】:
标签: python pandas web-scraping beautifulsoup