Python3 刮刀。直到最后才解析 xpath
Python3 scraper. Doesn't parse the xpath till the end
我正在使用 lxml.html 模块
from lxml import html
page = html.parse('http://directory.ccnecommunity.org/reports/rptAccreditedPrograms_New.asp?sort=institution')
# print(page.content)
unis = page.xpath('//tr/td[@valign="top" and @style="width: 50%;padding-right:15px"]/h3/text()')
print(unis.__len__())
with open('workfile.txt', 'w') as f:
for uni in unis:
f.write(uni + '\n')
这里 (http://directory.ccnecommunity.org/reports/rptAccreditedPrograms_New.asp?sort=institution#Z) 的网站上全是大学。
问题是它解析到字母 'H' (244 unis)。
我不明白为什么,因为我看到它解析所有 HTML 直到最后。
我还记录了我自己,244 不是列表的限制或 python3 中的任何内容。
那个 HTML 页面根本就不是 HTML,它完全坏了。但是下面会做你想做的。它使用 BeautifulSoup 解析器。
from lxml.html.soupparser import parse
import urllib
url = 'http://directory.ccnecommunity.org/reports/rptAccreditedPrograms_New.asp?sort=institution'
page = parse(urllib.request.urlopen(url))
unis = page.xpath('//tr/td[@valign="top" and @style="width: 50%;padding-right:15px"]/h3/text()')
有关详细信息,请参阅 http://lxml.de/lxmlhtml.html#really-broken-pages。
对于网络抓取,我建议您使用 BeautifulSoup 4
使用 bs4 这很容易做到:
from bs4 import BeautifulSoup
import urllib.request
universities = []
result = urllib.request.urlopen('http://directory.ccnecommunity.org/reports/rptAccreditedPrograms_New.asp?sort=institution#Z')
soup = BeautifulSoup(result.read(),'html.parser')
table = soup.find_all(lambda tag: tag.name=='table')
for t in table:
rows = t.find_all(lambda tag: tag.name=='tr')
for r in rows:
# there are also the A-Z headers -> check length
# there are also empty headers -> check isspace()
headers = r.find_all(lambda tag: tag.name=='h3' and tag.text.isspace()==False and len(tag.text.strip()) > 2)
for h in headers:
universities.append(h.text)
我正在使用 lxml.html 模块
from lxml import html
page = html.parse('http://directory.ccnecommunity.org/reports/rptAccreditedPrograms_New.asp?sort=institution')
# print(page.content)
unis = page.xpath('//tr/td[@valign="top" and @style="width: 50%;padding-right:15px"]/h3/text()')
print(unis.__len__())
with open('workfile.txt', 'w') as f:
for uni in unis:
f.write(uni + '\n')
这里 (http://directory.ccnecommunity.org/reports/rptAccreditedPrograms_New.asp?sort=institution#Z) 的网站上全是大学。
问题是它解析到字母 'H' (244 unis)。 我不明白为什么,因为我看到它解析所有 HTML 直到最后。
我还记录了我自己,244 不是列表的限制或 python3 中的任何内容。
那个 HTML 页面根本就不是 HTML,它完全坏了。但是下面会做你想做的。它使用 BeautifulSoup 解析器。
from lxml.html.soupparser import parse
import urllib
url = 'http://directory.ccnecommunity.org/reports/rptAccreditedPrograms_New.asp?sort=institution'
page = parse(urllib.request.urlopen(url))
unis = page.xpath('//tr/td[@valign="top" and @style="width: 50%;padding-right:15px"]/h3/text()')
有关详细信息,请参阅 http://lxml.de/lxmlhtml.html#really-broken-pages。
对于网络抓取,我建议您使用 BeautifulSoup 4 使用 bs4 这很容易做到:
from bs4 import BeautifulSoup
import urllib.request
universities = []
result = urllib.request.urlopen('http://directory.ccnecommunity.org/reports/rptAccreditedPrograms_New.asp?sort=institution#Z')
soup = BeautifulSoup(result.read(),'html.parser')
table = soup.find_all(lambda tag: tag.name=='table')
for t in table:
rows = t.find_all(lambda tag: tag.name=='tr')
for r in rows:
# there are also the A-Z headers -> check length
# there are also empty headers -> check isspace()
headers = r.find_all(lambda tag: tag.name=='h3' and tag.text.isspace()==False and len(tag.text.strip()) > 2)
for h in headers:
universities.append(h.text)