import pymysql
class MysqlHelper(object):
def __init__(self):
self.db = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='123456', database='py1011', charset='utf8')
self.cursor = self.db.cursor()
def execute_modify_sql(self,sql, data):
self.cursor.execute(sql, data)
self.db.commit()
def __del__(self):
self.cursor.close()
self.db.close()
if __name__ == '__main__':
conn = MysqlHelper()
import requests,re
def getdetail(url):
import mysqlhelper
myhelper = mysqlhelper.MysqlHelper()
sql = 'INSERT INTO yangguang (movie_title, movie_torrent) VALUES (%s, %s)'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36'
}
response = requests.get(url,headers=headers)
html = response.content.decode('gbk')
movie_title_name = re.search('<h1><font color=#07519a>(.*)</f',html)
movie_title = movie_title_name.group(1)
movie_torrent_url = re.search('ddf"><a href="(.*)">ft',html)
movie_torrent = movie_torrent_url.group(1)
movie_title_list = []
movie_title_list.append(movie_title)
movie_down_url = []
movie_down_url.append(movie_torrent)
movie_down_url_all = []
movie_down_url_all.append(movie_down_url)
data = (movie_title, movie_torrent)
myhelper.execute_modify_sql(sql, data)
print(movie_title)
print(movie_torrent)
def getpage():
for i in range(1,20):
lurl = 'http://www.ygdy8.com/html/gndy/dyzz/list_23_%s.html' % i
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36'
}
response = requests.get(lurl,headers=headers)
html = response.text
movie_url_list = re.findall('<a href="(.*)" class="ulink"',html)
for movie_item in movie_url_list:
movie_url = 'http://www.ygdy8.com'+movie_item
getdetail(movie_url)
if __name__ == '__main__':
getpage()