Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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
Python 尝试从csv文件中刮取多个URL。但除了从csv文件加载的最后一个url之外,所有url都面临404响应 导入请求 从bs4导入BeautifulSoup 导入csv 导入lxml 打开('xyz/spec.csv')作为文件: 需求数据=[] 对于文件中的行: headers={'User-Agent':'Mozilla/5.0'} r=requests.get(行,headers=headers) 汤=美汤(右文本,“lxml”) need=soup.find_all('span',attrs={“class”:“10965hju”}) 需要=[] 有需要的山雀: needs.append(tit.text.strip()) reqdata.append(需要) 打印(请求数据)_Python_Python 3.x_Web Scraping_Beautifulsoup_Httpresponse - Fatal编程技术网

Python 尝试从csv文件中刮取多个URL。但除了从csv文件加载的最后一个url之外,所有url都面临404响应 导入请求 从bs4导入BeautifulSoup 导入csv 导入lxml 打开('xyz/spec.csv')作为文件: 需求数据=[] 对于文件中的行: headers={'User-Agent':'Mozilla/5.0'} r=requests.get(行,headers=headers) 汤=美汤(右文本,“lxml”) need=soup.find_all('span',attrs={“class”:“10965hju”}) 需要=[] 有需要的山雀: needs.append(tit.text.strip()) reqdata.append(需要) 打印(请求数据)

Python 尝试从csv文件中刮取多个URL。但除了从csv文件加载的最后一个url之外,所有url都面临404响应 导入请求 从bs4导入BeautifulSoup 导入csv 导入lxml 打开('xyz/spec.csv')作为文件: 需求数据=[] 对于文件中的行: headers={'User-Agent':'Mozilla/5.0'} r=requests.get(行,headers=headers) 汤=美汤(右文本,“lxml”) need=soup.find_all('span',attrs={“class”:“10965hju”}) 需要=[] 有需要的山雀: needs.append(tit.text.strip()) reqdata.append(需要) 打印(请求数据),python,python-3.x,web-scraping,beautifulsoup,httpresponse,Python,Python 3.x,Web Scraping,Beautifulsoup,Httpresponse,因为您只是从csv文件中读取行,所以所有URL的末尾都有一个换行符(\n),最后一个除外 最简单的解决办法是 r=requests.get(line.strip(),headers=headers) 无参数删除前导空格和尾随空格。见下文 (如果您仍面临问题,则必须共享spec.csv) import requests from bs4 import BeautifulSoup with open('xyz/spec.csv') as file: reqdata = [] hea

因为您只是从csv文件中读取行,所以所有URL的末尾都有一个换行符(
\n
),最后一个除外

最简单的解决办法是

r=requests.get(line.strip(),headers=headers)

无参数删除前导空格和尾随空格。

见下文

(如果您仍面临问题,则必须共享
spec.csv

import requests
from bs4 import BeautifulSoup

with open('xyz/spec.csv') as file:
    reqdata = []
    headers = {'User-Agent': 'Mozilla/5.0'}
    for line in [l.strip() for l in file.readlines()]:
        r = requests.get(line, headers=headers)
        if r.status_code == 200:
            soup = BeautifulSoup(r.text, "lxml")
            need = soup.find_all('span', attrs={"class": "10965hju"})
            needs = []
            for tit in need:
                needs.append(tit.text.strip())
            reqdata.append(needs)

print(reqdata)