Python类型错误回溯(最近调用最后一次)

2024-05-14 08:51:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试建立一个爬虫,我想打印所有的链接上的网页 我使用的是python3.5

这是我的密码

import requests
from bs4 import BeautifulSoup
def crawler(link):
    source_code = requests.get(link)
    source_code_string = str(source_code)
    source_code_soup = BeautifulSoup(source_code_string,'lxml')
    for item in source_code_soup.findAll("a"):
        title = item.string
        print(title)

crawler("https://www.youtube.com/watch?v=pLHejmLB16o")

但我得到这样的错误

^{pr2}$

Tags: import网页密码sourcestringtitle链接link
1条回答
网友
1楼 · 发布于 2024-05-14 08:51:34

如果你是有意只打印标题的链接,你犯了一个小错误,替换行:

source_code_string = str(source_code)

使用

^{pr2}$

除此之外,代码看起来很好并且正在运行。 py.u文件让爬虫调用v1

import requests
from bs4 import BeautifulSoup
def crawler(link):
    source_code = requests.get(link)
    source_code_string = source_code.text 
    source_code_soup = BeautifulSoup(source_code_string,'lxml')
    for item in source_code_soup.findAll("a"):
        title = item.string
        print(title)


crawler("https://www.youtube.com/watch?v=pLHejmLB16o")

关于这个错误,如果你像这样正确地调用文件,你就不会得到这个错误

python3 wen_crawler_v1.py

相关问题 更多 >

    热门问题