Skip to main content
 首页 » 编程设计

python之如何使用 Python 3 构建网络爬虫

2025年05月04日16kuangbin

我一直在观看 thenewboston 的 3 个关于如何制作网络爬虫的视频。似乎它们已经过时并且链接不存在。如果有人能完成本教程的第一部分,我将不胜感激。这就是我所知道的。我尝试了不同的网站,但无济于事。

import requests 
from bs4 import BeautifulSoup 
 
def my_spider(max_pages): 
    page = 1 
    while page <= max_pages: 
        url = """Here I tried different websites""" + str(page) 
        source_code = requests.get(url) 
        plain_text = source_code.text 
        soup = BeautifulSoup(plain_text) 
        for link in soup.findALL("a", {"class" : "item-name"}): 
            href = """example site like following: https://example.com""" + link.get("href") 
            print(href) 
        page += 1 
 
print(my_spider(2)) 

例如,使用此代码我想抓取网站以获取标题链接或类似内容。

仅供引用,这是视频。 https://www.youtube.com/watch?v=sVNJOiTBi_8&list=PL6gx4Cwl9DGAcbMi1sH6oAMk4JHw91mC_&index=26

是教程25-27

提前致谢!

请您参考如下方法:

有用于网络抓取的 python 3 库,但没有一个像 python 2 库 Scrapy 一样流行,

一些可用的 python 3 Web Scrapers/Crawler: 1-排场 2- Pholcidae 3- pyburrow

我自己没用过,但你可以查看他们的网站或 github 上的源代码,以更好地了解你如何使用它们