我正在调用这样的类方法:
soup = bs4.BeautifulSoup(self.req_proxy.generate_proxied_request(some_url).text, "html.parser", "United States")
并且类方法定义如下:
def generate_proxied_request(self, url, params={}, req_timeout=30, country=None):
if country is not None:
searched_proxies = []
for proxy in self.proxy_list:
if str(proxy[1]) == country:
searched_proxies.append(proxy)
else:
searched_proxies = self.proxy_list
United States
旨在解析country
变量,但事实并非如此。调用该函数时,它采用值None
。为什么country
不反映United States
值?
答案 0 :(得分:1)
您正在将"United States"
传递给BeautifulSoup
构造函数,而不是传递到它应该去的generate_proxied_request
方法。
以下是正确的:
soup = bs4.BeautifulSoup(self.req_proxy.generate_proxied_request(some_url, country="United States").text, "html.parser")
BeautifulSoup
构造函数is defined喜欢:
def __init__(self, markup="", features=None, builder=None,
parse_only=None, from_encoding=None, **kwargs):
...
因此,在当前表单中,您将"United States"
分配给builder
参数。
答案 1 :(得分:0)
试试这个:
soup = bs4.BeautifulSoup(self.req_proxy.generate_proxied_request(some_url, country="United States").text, "html.parser")`