Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Web scraping 只返回txt文件的最后一个URL的漂亮汤_Web Scraping_Beautifulsoup_Python Requests - Fatal编程技术网

Web scraping 只返回txt文件的最后一个URL的漂亮汤

Web scraping 只返回txt文件的最后一个URL的漂亮汤,web-scraping,beautifulsoup,python-requests,Web Scraping,Beautifulsoup,Python Requests,我试图解析一组txt文件的url,但是Beauty Soup只返回最后一个url的内容。这是一组URL,包含来自网站LetterBoxD的电影评论。例如,如果该文件有10个URL,则前9个URL的值为“无”。只有10号能正常返回。有人能帮我吗 from bs4 import BeautifulSoup import requests with open('list_of_urls.txt', 'r') as f: x = f.readlines() for url in x: pag

我试图解析一组txt文件的url,但是Beauty Soup只返回最后一个url的内容。这是一组URL,包含来自网站LetterBoxD的电影评论。例如,如果该文件有10个URL,则前9个URL的值为“无”。只有10号能正常返回。有人能帮我吗

from bs4 import BeautifulSoup
import requests

with open('list_of_urls.txt', 'r') as f:
  x = f.readlines()

for url in x:
  page = requests.get(url)
  soup = BeautifulSoup(page.content, 'html.parser')
  text = soup.find(class_='review body-text -prose -hero -loose')
  print(text)

要通过几个类查找元素,应使用类名数组:

text = soup.find(class_= ['review', 'body-text', '-prose', '-hero', '-loose'])
同样,letterboxd.com在review元素上可能有不同的类组合,例如,
评论正文-散文-英雄美化
,因此我建议搜索较少的类,例如

text = soup.find(class_= ['review', 'body-text'])

非常感谢你!但是我发现URL的结尾有一个\n。因此,我使用rstrip('\n')将其删除

顺便说一句,Alexandra的提示对未来的提取有很大帮助!谢谢大家!

这是我的新代码:

for url in x:
  url = url.rstrip('\n')
  page = requests.get(url)
  soup = BeautifulSoup(page.content, 'html.parser')
  text = soup.find(class_='review body-text -prose -hero -loose')
  print(text)

也许您指定的类只存在于最后一个url的页面源中?介意分享这10个url的示例吗?