[英]How can I extract comments from all the links in a website Python
我試圖從網站上的幾個論壇中提取評論。 我有我希望從中提取評論的鏈接列表。 當我在代碼 (f"{i}/index{item}/")) 中給出單個鏈接而不是 {i} 時,代碼工作正常,但使用下面的代碼,它給出了一個空列表。
數據
name Link
a https://www.f150forum.com/f118/2019-adding-ada...
b https://www.f150forum.com/f118/2018-adding-ada...
c https://www.f150forum.com/f118/adaptive-cruise...
d https://www.f150forum.com/f118/2018-platinum-s...
e https://www.f150forum.com/f118/adaptive-cruise...
f https://www.f150forum.com/f118/adaptive-cruise...
我的代碼
link_url = []
username=[]
comments = []
for i in df['Link']:
with requests.Session() as req:
for item in range(1):
r = req.get(
f"{i}/index{item}/")
soup = BeautifulSoup(r.text, 'html.parser')
link_url.append(item)
for item in soup.findAll('div',attrs={"class":"ism-true"}):
result = [item.get_text(strip=True, separator=" ")]
comments.append(result)
for item in soup.findAll('a',attrs={"class":"bigusername"}):
name = [item.get_text(strip=True, separator=" ")]
username.append(name)
你能幫我解決這個問題嗎? 先感謝您。
好的,我看到您的鏈接在數據框中,您可以使用以下命令循環它們:
import pandas as pd
from io import StringIO
data = """
name,Link
a,https://www.f150forum.com/f118/2019-adding-ada...
b,https://www.f150forum.com/f118/2018-adding-ada...
c,https://www.f150forum.com/f118/adaptive-cruise...
d,https://www.f150forum.com/f118/2018-platinum-s...
e,https://www.f150forum.com/f118/adaptive-cruise...
"""
df = pd.read_csv(StringIO(data),sep=',')
for index, row in df.iterrows():
print(row['Link'])
結果 :
https://www.f150forum.com/f118/2019-adding-ada...
https://www.f150forum.com/f118/2018-adding-ada...
https://www.f150forum.com/f118/adaptive-cruise...
https://www.f150forum.com/f118/2018-platinum-s...
https://www.f150forum.com/f118/adaptive-cruise...
然后,將值(鏈接)放在您的請求中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.