蘑菇街
agent_list.py
import random
def get_random_agent():
agent_list = [
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
"Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
"Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
"Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
"Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
"Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
]
agent = random.choice(agent_list)
return agent
if __name__ == '__main__':
agent = get_random_agent()
print(agent)
数据库中创建的字段
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index
from sqlalchemy.orm import sessionmaker, relationship
import pymysql
from sqlalchemy import create_engine
engine = create_engine("mysql+pymysql://root:123456@127.0.0.1:3306/maoyan_db?charset=utf8", max_overflow=5,encoding='utf-8')
Base = declarative_base()
class MoguProduct(Base):
__tablename__ = 'mogu'
id = Column(Integer, primary_key=True, autoincrement=True) #主键,自增
tradeitemid = Column(String(128))
img = Column(String(1024))
clienturl = Column(String(1024))
link = Column(String(1024))
title = Column(String(512))
orgprice = Column(String(128))
price = Column(String(128))
similarityurl = Column(String(1024))
py
运行的程序
import json
import requests
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from agent_helper import get_random_agent
from models import MoguProduct
engine = create_engine("mysql+pymysql://root:123456@127.0.0.1/mogudb?charset=utf8", max_overflow=5)
session_maker = sessionmaker(bind=engine)
session = session_maker()
# 取页面HTML
def get_one_page(url, page):
agent = get_random_agent()
# referer = "https://list.mogujie.com/s?page=" + page + "&q=%E8%A1%A3%E6%9C%8D&sort=pop&ppath=&ptp=1.5y18ub.0.0.d9fJkbZi"
referer = "https://list.mogujie.com/s?page=" + str(page) + "&q=%E8%A1%A3%E6%9C%8D&sort=pop&ppath=&ptp=1.5y18ub.0.0.wVFuR4dg"
headers = {
'Referer': referer,
'User-Agent': agent,
'Host': 'list.mogujie.com',
'Accept': 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9',
'Connection': 'keep-alive',
'X-Requested-With': 'XMLHttpRequest',
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
text = response.content.decode('utf-8')
return text
return None
# 解析JSON数据
def parse_json(html):
# 匹配出现的第一个(
start = html.index('(') + 1
html = html[start:-2]
json_data = json.loads(html)
result_list = json_data['result']['wall']['docs']
for item in result_list:
mogu = MoguProduct()
mogu.tradeitemid = item['tradeItemId']
mogu.img = item['img']
mogu.clienturl = item['clientUrl']
mogu.link = item['link']
mogu.title = item['title']
mogu.orgprice = item['orgPrice']
mogu.price = item['price']
mogu.similarityurl = item['similarityUrl']
# todo 保存数据库
try:
session.add(mogu)
session.commit()
except Exception as e:
pass
def main():
for i in range(69):
page = i
print(i)
url = "https://list.mogujie.com/search?callback=jQuery211032322620930865265_1546578701309&_version=8193&ratio=3%3A4&cKey=43&sort=pop&page=" + str(i+1) + "&q=%25E8%25A1%25A3%25E6%259C%258D&minPrice=&maxPrice=&ppath=&cpc_offset=&ptp=1.5y18ub.0.0.y3JjPAiS&_=1546578701310"
# url = "https://list.mogujie.com/search?callback=jQuery211046561435892064384_1546568094492&_version=8193&ratio=3%3A4&cKey=43&sort=pop&page=" + str(i+1) + "&q=%25E8%25A3%25A4%2 5E5%25AD%2590&minPrice=&maxPrice=&ppath=&cpc_offset=&ptp=1.5y18ub.0.0.k1OJRbUq&_=1546568094493"
html = get_one_page(url, page)
print(html)
parse_json(html)
if __name__ == "__main__":
main()
数据库建模语句,SQL语句
create database mogudb default character set=utf8;
use mogudb;
create table mogu (
id integer auto_increment primary key,
tradeitemid varchar(128),
img varchar(1024),
clienturl varchar(1024),
link varchar(1024),
title varchar(512),
orgprice varchar(128),
price varchar(128),
similarityurl varchar(1024)
);
create unique index ux_mogu_tradeitemid on mogu(tradeitemid);