我正在尝试在Linux中运行此python代码(第二天同时使用linux和python)我从github复制了该程序,并且需要更改标头,因为我使用的服务器使用Firefox 60.2.2,我正尝试收集网络上的图像并将其保存到文件中。但是,我收到HTTP错误400:错误的请求。 如果您有任何解决方案,请解释一下,因为我是该程序的新手。
代码:
#!/usr/bin/env python
import os
import urllib.request as ulib
from bs4 import BeautifulSoup as Soup
import json
url_a = 'https://www.google.com/search?ei=1m7NWePfFYaGmQG51q7IBg&hl=en&q={}'
url_b = '\&tbm=isch&ved=0ahUKEwjjovnD7sjWAhUGQyYKHTmrC2kQuT0I7gEoAQ&start={}'
url_c = '\&yv=2&vet=10ahUKEwjjovnD7sjWAhUGQyYKHTmrC2kQuT0I7gEoAQ.1m7NWePfFYaGmQG51q7IBg'
url_d = '\.i&ijn=1&asearch=ichunk&async=_id:rg_s,_pms:s'
url_base = ''.join((url_a, url_b, url_c, url_d))
headers = {'User-Agent':'Mozilla/5.0 (X11; Linux i986; rv:60.0) Gecko/20200101 Firefox/60.0'}
def get_links(search_name):
search_name = search_name.replace(' ', '+')
url = url_base.format(search_name, 0)
request = ulib.Request(url, None, headers)
json_string = ulib.urlopen(request).read()
page = json.loads(json_string)
new_soup = Soup(page[1][1], 'lxml')
images = new_soup.find_all('img')
links = [image['src'] for image in images]
return links
def save_images(links, search_name):
directory = search_name.replace(' ', '_')
if not os.path.isdir(directory):
os.mkdir(directory)
for i, link in enumerate(links):
savepath = os.path.join(directory, '{:06}.png'.format(i))
ulib.urlretrieve(link, savepath)
if __name__ == '__main__':
search_name = 'auv side scan sonar sunken ship'
links = get_links(search_name)
save_images(links, search_name)
(tensorflow) [usr-login2 darkflow-master]$ ./get_jimages.py
Traceback (most recent call last):
File "./get_jimages.py", line 40, in <module>
links = get_links(search_name)
File "./get_jimages.py", line 20, in get_links
json_string = ulib.urlopen(request).read()
File "/home/usr/anaconda3/envs/tensorflow/lib/python3.6/urllib/request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "/home/usr/anaconda3/envs/tensorflow/lib/python3.6/urllib/request.py", line 532, in open
response = meth(req, response)
File "/home/usr/anaconda3/envs/tensorflow/lib/python3.6/urllib/request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "/home/usr/anaconda3/envs/tensorflow/lib/python3.6/urllib/request.py", line 570, in error
return self._call_chain(*args)
File "/home/usr/anaconda3/envs/tensorflow/lib/python3.6/urllib/request.py", line 504, in _call_chain
result = func(*args)
File "/home/usr/anaconda3/envs/tensorflow/lib/python3.6/urllib/request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request