我试图建立一个在线论坛的网址列表。在我的情况下使用BeautifulSoup是必要的。目标是一个包含一个线程的每个页面的url列表,例如
[http://forum.pcgames.de/stellt-euch-vor/9331721-update-im-out-bitches.html,
http://forum.pcgames.de/stellt-euch-vor/9331721-update-im-out-bitches-2.html,
http://forum.pcgames.de/stellt-euch-vor/9331721-update-im-out-bitches-3.html]
工作原理如下:
#import modules
import requests
from bs4 import BeautifulSoup
#define main-url
url = 'http://forum.pcgames.de/stellt-euch-vor/9331721-update-im-out-bitches.html'
#create a list of urls
urls=[url]
#load url
page = requests.get(url)
#parse it using BeautifulSoup
soup = BeautifulSoup(page.text, 'html.parser')
#search for the url of the next page
nextpage = soup.find("a", ["pag next"]).get('href')
#append the urls of the next page to the list of urls
urls.append(nextpage)
print(urls)
当我尝试为下面的页面构建if循环时,它将不起作用。为什么?你知道吗
for url in urls:
urls.append(soup.find("a", ["pag next"]).get('href'))
("a", ["pag next"]).get('href')
标识下一页的url)
这不是一个使用url分页的选项,因为将有许多其他线程进行爬网。 我正在MacBookPro上使用Jupyter笔记本服务器5.7.4 Python 3.7.1版 IPython 7.2.0版
我知道thispost的存在。对于我的初学者来说,代码写得太复杂了,但是也许你的经验可以让你把它应用到我的用例中。你知道吗
分页的url模式始终与此站点一致,因此不需要请求获取页面url。相反,您可以解析按钮中显示“第1页,共10页”的文本,并在知道最终页码后构建页面URL。你知道吗
您正在遍历url并将其添加到自身中,因此列表的大小将无限期地继续增加。你知道吗
您正在将每个url从一个url添加到另一个url-您看到问题了吗?URL继续增长。。。它里面已经有了你正在迭代的每个url。您的意思是调用一个函数,在下一个url上执行前面的代码,然后将其添加到列表中吗?你知道吗
相关问题 更多 >
编程相关推荐