loops - 抓取“下一页”BeautifulSoup 或 Scrapy?
问题描述
我正在做我的第一个真正的项目。我正在尝试从rotoworld 的新闻提要中抓取所有 nfl 球员新闻:
我已经成功地使用 BeautifulSoup 中的 bs4 从第一页中提取了我想要的所有信息,但我正在研究如何从“旧”选项卡中访问信息。我认为如果每次打开新页面时 url 都会更改,那么执行此操作会很容易,但事实并非如此。我想知道是否有人对使用 BS 抓取“下一页”有任何提示,或者我是否应该尝试像 scrappy 这样的程序?
我正在使用 python 3。这是我为感兴趣的人准备的代码。
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url="http://www.rotoworld.com/playernews/nfl/football/"
# opening up connection, grabing the page
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
#html parsing
page_soup = soup(page_html, "html.parser")
#grabs each news report
containers = page_soup.findAll("div",{"class":"pb"})
filename = "nfl_player_news.csv"
f = open(filename, "w")
headers = "Player, Position, Team, Report, More Info, date\n"
f.write("")
for container in containers:
ugly_player_info = container.div.div.text.strip("\r\n")
neat_player_info = " ".join(ugly_player_info.split())
player = container.div.div.a.text
position = " ".join(neat_player_info.split()[3:4])
team = " ".join(neat_player_info.split()[5:])
report = container.p.text.strip()
more_info = container.findAll("div",{"class":"impact"})
info = more_info[0].text.strip()
date_messy = container.findAll("div",{"class":"date"})
date_time = date_messy[0].text.strip()
ny_date= " ".join(date_time.split()[0:2])
date = ny_date + " 2018"
print("player" + player)
print("position" + position)
print("team" + team)
print("report" + report)
print("info" + info)
print("date" + date)
f.write(player + "," + position + "," + team + "," + report.replace(",", "|") + "," + info.replace(",","|") + "," + date + "\n")
f.close()
解决方案
推荐阅读
- java - 沃森语音到文本未经授权的请求错误 401
- excel - K - 12 年级的排序列 --- 我可以让 Excel 认为“K”是零吗?
- java - 如何使用 Spring Cloud Contract WireMock
- java - ModelMapper:尝试将方法替换为映射器
- java - 在netbeans ide中运行java代码的问题
- python - 如何将十六进制解释为无符号长整数(20 位整数、12 位小数、模 20 位)
- regex - 替换两个字符串之间行中最后出现的字符串
- java - 带有 Junit5 的 Mockito.spy
- excel - 有没有办法在 excel 中使用 VLOOKUP 来计算表中的某些值,但仅限于 VLOOKUP 索引的某个范围?
- git - 如何使用默认的 git clone 语句代替个人访问令牌?