我被我的网络爬虫暂时塞住了。到目前为止,守则是:
import requests
from bs4 import BeautifulSoup
def search_spider(max_pages):
page = 1
while page <= max_pages:
url = 'https://www.thenewboston.com/search.php?type=1&sort=pop&page=' + str(page)
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
for link in soup.findAll('a', {'class': 'user-name'}):
href = "https://www.thenewboston.com/" + link.get('href')
print(href)
search_spider()这是YT教程中的一个例子。有人知道当我没有像1,2,3这样的网站结尾时,我该如何修改代码吗?但是像021587,0874519,NI875121这样的数字呢?anker网站域总是相同的,但结尾并不像本例中那样直接。因此,我需要知道的是,如何为str(page)插入一个变量,该变量可以从我计算机上的.txt文件(几百个)或当我将它们复制并粘贴到代码中时从列表中获取结束编号的网站?当然,当到达列表的末尾时,Python应该停止。
因为我对python非常了解,我目前还不知道如何解决这个问题。如果您需要进一步的信息,请告诉我。感谢你的回应!
浮点
发布于 2016-05-01 20:56:28
好吧,如果您有一个想要访问的页面列表,而不是一系列的数字,您可以这样做:
pages = ['021587', '0874519', 'NI875121']
for page in pages:
url = 'http://example.com/some-path/' + str(page)从文件中读取:
with open('filename.txt') as f:
contents = f.read()假设页面由空格分隔,则可以运行
pages = contents.split()https://stackoverflow.com/questions/36971787
复制相似问题