Cannot crawl and access a particular div in the text file

☆樱花仙子☆ 提交于 2019-12-13 21:21:13

问题


I have the following code and I want to access the text of a particular div.

from bs4 import BeautifulSoup
import requests
import urlparse
example = open('example.txt')
html = example.read()



def gettext(htmltext):
    soup=BeautifulSoup(htmltext, "lxml")
    for div in soup.findAll('div', attrs={'class':'_5pbx userContent'}):
        print div.text

gettext(html)

At first, I tried it through a link to a facebook profile but it didn't work. But now I copied the whole source code and saved it in example.txt file. Still, it cannot access the div with class _5pbx usercontent. You can download the source at

view-source:https://www.facebook.com/abhas.mittal7?fref=ts

Please make an example.txt file and try running my code. I don't know what's the problem. Kindly help me out.

来源:https://stackoverflow.com/questions/32312640/cannot-crawl-and-access-a-particular-div-in-the-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!