6.4 KiB
6.4 KiB
In [ ]:
import requests
import time
from bs4 import BeautifulSoup
import pymongo
import re
#m_mongo = {}
#requests.packages.urllib3.disable_warnings()
#requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += 'HIGH:!DH:!aNULL'
#myclient = pymongo.MongoClient('mongodb://localhost:27017/')
#mydb = myclient["gaokao"]
#mycol = mydb["news"]
url = 'data/京剧剧本 - 总目.html'
a_url = 'https://scripts.xikao.com'
#strhtml = requests.get(url)
strhtml = 'data/京剧剧本 - 总目.html'
#strhtml.encoding = 'utf8'
soup = BeautifulSoup(strhtml,'html.parser')
data = soup.select('#master-table')
for item in data:
print(item)
print('\br')In [139]:
from bs4 import BeautifulSoup
import re
with open("data/京剧剧本 - 总目.html") as fp:
soup = BeautifulSoup(fp, 'lxml')
#soup = BeautifulSoup("<html>a web page</html>", 'html.parser')
data = soup.find_all(id = 'master-table')
for item in data:
list1 = item.find_all(class_=re.compile("row_"))
#list1 = item.find_all(lambda tag: tag.get('class') == ['row_c'] or tag.get('class') == ['row_a'])
#print(list1)
for tr in list1:
#print(tr)
if tr.a:
print(tr.a.get('href'),tr.b.text)
#print('end')[0;31m---------------------------------------------------------------------------[0m
[0;31mFeatureNotFound[0m Traceback (most recent call last)
Cell [0;32mIn[139], line 5[0m
[1;32m 2[0m [38;5;28;01mimport[39;00m [38;5;21;01mre[39;00m
[1;32m 4[0m [38;5;28;01mwith[39;00m [38;5;28mopen[39m([38;5;124m"[39m[38;5;124mdata/京剧剧本 - 总目.html[39m[38;5;124m"[39m) [38;5;28;01mas[39;00m fp:
[0;32m----> 5[0m soup [38;5;241m=[39m [43mBeautifulSoup[49m[43m([49m[43mfp[49m[43m,[49m[43m [49m[38;5;124;43m'[39;49m[38;5;124;43mlxml[39;49m[38;5;124;43m'[39;49m[43m)[49m
[1;32m 7[0m [38;5;66;03m#soup = BeautifulSoup("<html>a web page</html>", 'html.parser')[39;00m
[1;32m 8[0m data [38;5;241m=[39m soup[38;5;241m.[39mfind_all([38;5;28mid[39m [38;5;241m=[39m [38;5;124m'[39m[38;5;124mmaster-table[39m[38;5;124m'[39m)
File [0;32m~/data/lib/python3.10/site-packages/bs4/__init__.py:250[0m, in [0;36mBeautifulSoup.__init__[0;34m(self, markup, features, builder, parse_only, from_encoding, exclude_encodings, element_classes, **kwargs)[0m
[1;32m 248[0m builder_class [38;5;241m=[39m builder_registry[38;5;241m.[39mlookup([38;5;241m*[39mfeatures)
[1;32m 249[0m [38;5;28;01mif[39;00m builder_class [38;5;129;01mis[39;00m [38;5;28;01mNone[39;00m:
[0;32m--> 250[0m [38;5;28;01mraise[39;00m FeatureNotFound(
[1;32m 251[0m [38;5;124m"[39m[38;5;124mCouldn[39m[38;5;124m'[39m[38;5;124mt find a tree builder with the features you [39m[38;5;124m"[39m
[1;32m 252[0m [38;5;124m"[39m[38;5;124mrequested: [39m[38;5;132;01m%s[39;00m[38;5;124m. Do you need to install a parser library?[39m[38;5;124m"[39m
[1;32m 253[0m [38;5;241m%[39m [38;5;124m"[39m[38;5;124m,[39m[38;5;124m"[39m[38;5;241m.[39mjoin(features))
[1;32m 255[0m [38;5;66;03m# At this point either we have a TreeBuilder instance in[39;00m
[1;32m 256[0m [38;5;66;03m# builder, or we have a builder_class that we can instantiate[39;00m
[1;32m 257[0m [38;5;66;03m# with the remaining **kwargs.[39;00m
[1;32m 258[0m [38;5;28;01mif[39;00m builder [38;5;129;01mis[39;00m [38;5;28;01mNone[39;00m:
[0;31mFeatureNotFound[0m: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?In [ ]: