Errno 11001 getaddrinfo 在循环访问 url 时失败



我想从某些页面中提取文档列表。

当我尝试循环访问网址列表时遇到问题,因为我不断得到

Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

请澄清为什么会这样。

如果我针对一个 url 页面运行,应该没有问题。

我有一个单独的代码使用Selenium/Webdriver,但是使用Selenium的问题是不同文件类型的下载行为。

例如,如果一个 url 将您带到一个 pdf 文件,它将打开一个新页面,显示完整的 pdf 文件。如果 URL 链接到 Excel 文件,则行为会有所不同。

更多细节可以在这里找到 如何控制硒 PDF 和 Excel 文件下载行为?

我最终得到了下面建议的代码,虽然它可能不使用Selenium,但它可以完成获取所有文件的工作。

谢谢!

import requests
from bs4 import BeautifulSoup
import re
import pandas as pd
import os
doc_urls = ['http://www.ha.org.hk/haho/ho/bssd/18G042Pc.htm'
'http://www.ha.org.hk/haho/ho/bssd/HKWCT03018A2Pa.htm',
'http://www.ha.org.hk/haho/ho/bssd/19D070Pa.htm',
'http://www.ha.org.hk/haho/ho/bssd/NTECT6AQ011Pa.htm',
'http://www.ha.org.hk/haho/ho/bssd/T18G052Pa.htm',
]
base_url = "http://www.ha.org.hk"

for doc in doc_urls:
with requests.Session() as session:
r = session.get(doc)
# get all documents links
docs = BeautifulSoup(r.text, "html.parser").select("a[href]")
print('Visiting:',doc)
for doc in docs:
href = doc.attrs["href"]
name = doc.text
print(f">>> Downloading file name: {name}, href: {href}")
# open document page
r = session.get(href)
# get file path
# check for attibute, if not, file doesn't exist: contact admin. but how to contact the hospital admin?
if hasattr(re.search("(?<=window.open\(')(.*)(?=',)", r.text), 'group'):
file_path = re.search("(?<=window.open\(')(.*)(?=',)", r.text).group(0)
print(file_path)
file_name = file_path.split("/")[-1]
# get file and save
r = session.get(f"{base_url}/{file_path}")
with open('C:\Users\tender_documents\'+ today_yyMMddhh + '\' + file_name, 'wb') as f:
f.write(r.content)
else:
print(f">>> File name: {name}, href: {href}", " is missing")
continue

这只是一个错字,您正在尝试使用整个正则表达式匹配:

r = session.get(f"{base_url}/{file_path}")

应该是

r = session.get(f"{base_url}/{file_name}")

最新更新