如何使用Python Beautifulsoup添加requests.get()中的标签和字符串



这是我的代码:

from bs4 import BeautifulSoup
import requests
import csv
import pandas as pd
links = pd.read_csv('C:\Users\acer\Desktop\links.csv',encoding = 'utf-8',dtype=str)
for i in range(1,10):
    link = links.iloc[i,0]
    for count in range(1,5):
        r = requests.get(link + str(count))
        soup = BeautifulSoup(r.text,'lxml')
        ##comp links
        for links in soup.find_all('th',{"id":"c_name"}):
            link = links.find('a')
            li = link['href'][3:]
            print("https://www.hindustanyellowpages.in/Ahmedabad/" + li)

我遇到以下错误:

TypeError: :'tag'和'str''

的不支持操作数类型

在源代码的最后一行中。您将" TAG"类(LI)实例与具有URL的字符串进行了串联。

尝试在串联之前从标签中提取所需的信息。例如,如果要在链接中获取文本,请使用此。

print("https://www.hindustanyellowpages.in/Ahmedabad/" + li.text)

最新更新