在 thenewboston 的帮助下,我能够在 python 中创建一个不错的小型网络爬虫。看完他的视频后,我玩弄了它并添加了一些东西。我试图让它无限,因为它会记录每个链接上的每个链接,但我没有这样做。我也有多次记录同一链接的问题?我将如何解决这个问题?
这是我的代码。
import requests
from bs4 import BeautifulSoup
def spider(max_pages):
page = 1
while page <= max_pages:
url = ''
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
for link in soup.findAll("a"):
href = link.get("href")
title = link.get("title")
links = []
#print(href)
#print(title)
try:
get_single_user_data(href)
except:
pass
page += 1
def get_single_user_data(user_url):
source_code = requests.get(user_url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
#for item_name in soup.findAll('span', {'id':'mm-saleDscPrc'}):
# print(item_name.string)
for link in soup.findAll("a"):
href = link.get("href")
print(href)
spider(1)