Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.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 尽管文本存在,但找不到它_Python_Web Scraping_Beautifulsoup_Find - Fatal编程技术网

Python 尽管文本存在,但找不到它

Python 尽管文本存在,但找不到它,python,web-scraping,beautifulsoup,find,Python,Web Scraping,Beautifulsoup,Find,我正在用Python学习网页抓取。我编写代码从一个印度黄页中检索公司名称 r = requests.get("http://xyzxyz", headers={'User-Agent' : "Magic Browser"}) soup= BeautifulSoup(r.content,"html.parser") for link in soup.findAll("div", {"class"

我正在用Python学习网页抓取。我编写代码从一个印度黄页中检索公司名称

r =  requests.get("http://xyzxyz", headers={'User-Agent' : "Magic Browser"})
soup= BeautifulSoup(r.content,"html.parser")

for link in soup.findAll("div", {"class" : "col-sm-5"}):
    coLink = link.find("span" , {"class" : "jcn"})
    companyName = coLink.find("a").text
我收到错误“AttributeError:'非类型'对象没有属性'find'”。我知道,如果对象找不到任何东西,就会出现此错误。但是,如果我打印(coLink),它会在每个span类中提供以下链接

<span class="jcn"><a href="http://xyz/Kolkata/Sunrise-International-&lt;near&gt;-B-R-B-Basu-Road-/033P3001041_BZDET?xid=S29sa2F0YSBUYXBlciBSb2xsZXIgQmVhcmluZyBEZWFsZXJz" onclick="_ct('clntnm', 'lspg');" title="Sunrise International in , Kolkata">Sunrise International</a>
</span>
<span class="jcn"><a href="http://xyz/Kolkata/Shree-Shakti-Vyapaar-PVT-LTD/033P6001995_BZDET?xid=S29sa2F0YSBUYXBlciBSb2xsZXIgQmVhcmluZyBEZWFsZXJz" onclick="_ct('clntnm', 'lspg');" title="Shree Shakti Vyapaar PVT LTD in , Kolkata">Shree Shakti Vyapaar PVT LTD</a>
</span>


您可以帮助获取公司文本吗?

不解释当前问题,但提供替代解决方案-您可以使用与所需
a
元素匹配的:

for link in soup.select(".col-sm-5 .jcn a"):
    print(link.get_text())
这样,如果
span
中没有
a
链接,则不会出现任何错误


请注意,
col-sm-5
是一个用于定位元素的糟糕类名-它是特定于UI/布局的,很有可能被更改。

您确定所有
span.jcn
标记都包含在
a
标记中吗?因此添加简单异常将通过忽略所有没有标记的span.jcn来解决此问题在任何情况下,您都应该首先检查
coLink.find(“a”)
是否返回可以获取文本的标记,或者它是否返回
None
@ChristosPapoulas正如我在问题中提到的,我已经尝试了打印(coLink)。我还提供了该打印的输出。你们可以在输出中看到“a”,很好!记得投票给评论!感谢更正代码中的小语法:r=requests.get(“,headers={'User-Agent':“Magic Browser”})soup=BeautifulSoup(r.content,“html.parser”)作为soup中的链接。选择({.col-sm-5.jcn a“}):打印(link.get_text())现在它给出了“'set'对象没有'read'属性”。@BhaveshGhodasara是的,修正了输入错误。但是您得到的这个错误可能与提供的代码无关。回溯(最后一次调用):文件“g:\pyt\justdial.py”,第15行,在soup中的链接中。选择({.col-sm-5.jcn a“}):文件“C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site packages\bs4\element.py”,第1349行,在select tokens=shlex.split中(选择器)文件“C:\Users\user\AppData\Local\Programs\Python35\lib\shlex.py”,第273行,在拆分返回列表(lex)文件“C:\Users\user\AppData\Local\Programs\Python35\lib\shlex.py”,第263行,在下一个token=self.get_token()文件“C:\Users\user\AppData\Local\Programs\Python35\lib\shlex.py”,第90行,在get_token raw=self.read_token()文件“C:\Users\user\AppData\Local\Programs\Python\Python35\lib\shlex.py”的第118行,在read_token nextchar=self.instream.read(1)中AttributeError:“set”对象没有属性“read”……它仅为循环显示。@BhaveshGhodasara啊,当然,你已经错误地修复了打字错误,请删除大括号。再次为打字错误感到抱歉。