Python实战计划学习作业2-2
来源:互联网 发布:mac邮件图片显示问号 编辑:程序博客网 时间:2024/05/22 13:55
from bs4 import BeautifulSoupimport requestsimport pymongofrom string import whitespaceclient = pymongo.MongoClient('localhost', 27017)phone_number_sale = client['phone_number_sale']page_url_tab = phone_number_sale['page_url_tab']urls = ["http://bj.58.com/shoujihao/pn{}/".format(str(i)) for i in range(2, 200, 1)]urls.insert(0, "http://bj.58.com/shoujihao/")headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) ' + 'Chrome/54.0.2840.87 Safari/537.3',}def get_title_and_links(page_url): page_data = requests.get(page_url, headers=headers) soup = BeautifulSoup(page_data.text, 'lxml') amount = soup.select("#infocont > span > b")[0].get_text() if '0' == amount: return False else: titles = soup.select('#infolist div.boxlist div.boxlist ul li a.t strong.number') grid_urls = soup.select('#infolist div.boxlist div.boxlist ul li a.t') for title, gird_url in zip(titles, grid_urls): if gird_url.get("href").startswith('http://jump.zhineng.'): continue data = { 'Title': title.get_text(), 'Grid URL': gird_url.get("href"), } page_url_tab.insert_one(data) return Truedef check_each_grid(grid_url): page_data = requests.get(grid_url, headers=headers) soup = BeautifulSoup(page_data.text, 'lxml') page_title = soup.select('div.col_sub.mainTitle > h1')[0].get_text() price = soup.select('ul.suUl span.price.c_f50')[0].get_text() seller = soup.select('ul.vcard > li > a')[0].get_text() data = { 'Page title': page_title.translate(dict.fromkeys(map(ord, whitespace))), 'Price': price.translate(dict.fromkeys(map(ord, whitespace))), 'Seller': seller.translate(dict.fromkeys(map(ord, whitespace))) } return datadef feed_the_database(): for page in page_url_tab.find(): data = check_each_grid(page['Grid URL']) for field in data: page_url_tab.update_one({'_id': page['_id']}, {'$set': {field: data[field]}})if __name__ == '__main__': counter = 1 for url in urls: if not get_title_and_links(url): break else: print(counter) counter += 1 feed_the_database()
运行过程中卡在feed_the_database()函数里对第265个页面做分析的时候,问题待检查。得到的数据库内容大致如下。
0 0
- Python实战计划学习作业2-2
- Python实战计划学习作业1-2
- Python实战计划学习作业2-1
- Python实战计划学习作业1-1
- Python实战计划学习作业1-3
- Python实战计划学习作业1-4
- Python实战计划学习作业1-5
- Python学习-课后作业2
- python机器学习实战2:实现决策树
- python学习日记-实战(2)
- PYTHON实战开发作业 -WEEK1
- python作业2
- Python 作业2
- python作业2
- python-作业2
- Python学习day3作业
- Python学习day4作业
- Python学习day5作业
- Android客户端和服务端如何使用Token和Session
- Java 反射总结和使用
- 学习Token
- 【备忘】北风网-手机进销存系统 共80课时 下载
- 坚持#第118天~按每日安排来!
- Python实战计划学习作业2-2
- centos6 python 安装 sqlite 解决 No module named ‘_sqlite3′
- 自定义ViewGroup之拖拽图片显示(不知道起什么名字..)
- 异常控制流
- PAT-basic level-1008
- c++中类的大小
- java中insteanceof运算符
- field module的on input和on request区别
- Qt 之 模仿迅雷 根据Url获取文件信息——上