from bs4 import BeautifulSoup
import re
import urllib.request
import xlwt
import sqlite3
def main():
baseurl = "https://movie.douban.com/top250?start="
datalist = getData(baseurl)
savepath = ".\\豆瓣电影top250.xls"
askURL("https://movie.douban.com/top250?start=0")
findlink = re.compile(r'<a href="(.*?)">')
findImgSrc = re.compile(r'<img.*src="', re.S)
findTitle = re.compile(r'<span class="title">(.*)</span>')
findRating = re.compile(r'<span class="rating_num" property="v:average">.*</span>')
findJudge = re.compile(r'<span>(\d*人评价)</span>')
findInq = re.compile(r'<span class="inq">(.*)</span>')
findBd = re.compile(r'<p class="">(.*)</p>', re.S)
def getData(baseurl):
datalist = []
for i in range(0, 1):
url = baseurl + str(i * 25)
html = askURL(url)
soup = BeautifulSoup(html, "html.parser")
for item in soup.find_all('div', class_="item"):
data = []
item = str(item)
link = re.findall(findlink, item)[0]
print(link)
return datalist
def askURL(url):
head = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36 Edg/93.0.961.52"}
request = urllib.request.Request(url, headers=head)
html = ""
try:
response = urllib.request.urlopen(request)
html = response.read().decode("utf-8")
except urllib.error.URLError as e:
if hasattr(e, "code"):
print(e.code)
if hasattr(e, "reason"):
print(e.reason)
return html
def saveData(dbpath):
print("Saved")
if __name__ == "__main__":
main()
|