Q
与蟒蛇
1
A
回答
1
For循环很好,但你不能总是使用它们。在这种情况下,我只需重复按下“下一页”按钮中的链接,直到没有这样的按钮。事情是这样的:
url = <first page>
while True:
# extract data
if <there is a next page button>:
url = <href of the button>
else:
break
1
这将让所有的页面,产生的每一个BeautifulSoup对象,链接到下一个页面与类向前锚标记:
import requests
from urlparse import urljoin
def get_pages(base, url):
soup = BeautifulSoup(requests.get(url).content)
yield soup
next_page = soup.select_one("a.forward")
for page in iter(lambda: next_page, None):
soup = BeautifulSoup(requests.get(urljoin(base, page["href"])).content)
yield soup
next_page = soup.select_one("a.forward")
for soup in get_pages("https://www.xrel.to/", "https://www.xrel.to/games-release-list.html?archive=2016-01"):
print(soup)
+0
thx给你。但是我已经在我自己的解决方案中实现了Alex的想法。我在前面的课上做了这个,虽然 – Sannin
+0
@Sannin,在另一个说明中,你应该确实检查你是否正确地获取源并捕获连接错误,仅仅因为你没有发现该按钮并不一定意味着它是因为你到达最后一页 –
相关问题
- 1. 与蟒蛇
- 2. Pyevolve与蟒蛇
- 3. batch.models.PoolAddParameter与蟒蛇
- 4. 与蟒蛇
- 5. 与LXML蟒蛇
- 6. 与蟒蛇
- 7. 与蟒蛇
- 8. 与蟒蛇
- 9. 与蟒蛇
- 10. 与蟒蛇
- 11. 与蟒蛇
- 12. 与蟒蛇
- 13. 与蟒蛇
- 14. Cx_oracle:TO_DATE与蟒蛇
- 15. 与蟒蛇
- 16. 与蟒蛇
- 17. 与蟒蛇
- 18. 与蟒蛇
- 19. 与蟒蛇
- 20. 与蟒蛇
- 21. 与蟒蛇
- 22. 与蟒蛇
- 23. 与蟒蛇
- 24. 与蟒蛇DOC
- 25. 与蟒蛇
- 26. 与蟒蛇
- 27. 与蟒蛇
- 28. 与蟒蛇
- 29. 蟒蛇 - 与networkX
- 30. 与蟒蛇
THX 。非常好的想法=)我会尝试这个 – Sannin