gpt4 book ai didi

python - 未找到 '%s'“% url 的连接适配器

转载 作者:太空宇宙 更新时间:2023-11-03 14:21:09 24 4
gpt4 key购买 nike

很抱歉问这个问题!我是新手,所以请随意教我你们知道的任何东西。我正在制作一个用于营销目的的抓取工具,以从网站上抓取联系信息。我正在使用Python 3这是我的代码:

import requests, bs4, os, codecs, csv
import pandas as pd
import sys

os.path.join('usr', 'bin', 'spam')
openFile = open('C:\\Users\\hdtra\\Desktop\\Test_1.csv',encoding='utf-8-sig')

read_test = csv.reader(openFile)


for link in read_test :
res = requests.get(link)
res.raise_for_status
facebookSpider = bs4.BeautifulSoup(res.text)
email = facebookSpider.select("._4-u2._3xaf._3-95._4-u8")
helloFile = open('C:\\Users\\hdtra\\Desktop\\In processing\\information.txt','w')
helloFile.write(str(email[3].encode('utf-8')) + '\n')
helloFile.close()

不知道为什么它让我这样:

Traceback (most recent call last):
File "C:\Users\hdtra\Desktop\In processing\Facebook_spider.py", line 12, in <module>
res = requests.get(link)
File "C:\Program Files\Python36\lib\site-packages\requests\api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "C:\Program Files\Python36\lib\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Program Files\Python36\lib\site-packages\requests\sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "C:\Program Files\Python36\lib\site-packages\requests\sessions.py", line 612, in send
adapter = self.get_adapter(url=request.url)
File "C:\Program Files\Python36\lib\site-packages\requests\sessions.py", line 703, in get_adapter
raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for '['http://www.facebook.com/D2Streetwear/?ref=br_rs']'

我知道 get() 只能获取字符串,但不知道如何将这些链接转换为字符串。这是我的 cvs 文件:

只有一列 5 行:

http://www.facebook.com/D2Streetwear/?ref=br_rs
https://www.facebook.com/RealClothes/?ref=br_rs
https://www.facebook.com/Lecamelliaclothing/?ref=br_rs
https://www.facebook.com/TaTclothing-285844471884952/?ref=br_rs
https://www.facebook.com/Dai-Clothing-130675847640538/?ref=br_rs

我尝试输入 str(link()) 但它不起作用。

最佳答案

你应该明白csv.reader返回一个迭代器,该迭代器迭代每一行以返回每一行的列列表。

csv.reader(csvfile, dialect='excel', **fmtparams)

Return a reader object which will iterate over lines in the givencsvfile.[...]
Each row read from the csv file is returned as a list of strings.

大胆强调我的。您的 CSV 似乎包含单列,因此您可以使用 link[0] 访问第一列。

with open('test.csv') as f:
r = csv.reader(f)

for row in r:
r = requests.get(row[0])
...

我认为在处理文件 I/O 时始终使用 with...as 上下文管理器是一种很好的做法,因为它会自动关闭文件并生成更干净的代码。

关于python - 未找到 '%s'“% url 的连接适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47936244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com