问题
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