首页 > 编程知识 正文

python基础知识点总结,python爬虫经典例子

时间:2023-05-06 19:00:03 阅读:146367 作者:708

一种简单的爬行动物

方法一: fromseleniumimportwebdriver # web驱动程序对象驱动程序=web驱动程序. Firefox (# ) 打开网页driver.get (https://www.Sina.com.cn print (html ) fromlxmlimportetreee _ html=etree.html (html ) a=e _ com @href'(forIina:print (新闻: ),I ) for j in b: print (新闻链接: ),j ) driver.close ) )方法2:importcharder x64 ) chrome/65.0.3325.181 ' } rqg=requests.get (URL, headers=ua (rqg.encoding=chardet.detect ) rqg.content ) ['encoding'] html=rqg.content.decode (utf-8 parser=etree.html parser (encoding=' utf-8 ' ) ) content=html.XPath () Li内容(foriincontent : print ) I )3.将数据传输到数据库:检索#1数据库, 使用mongo客户端实例的属性访问数据库importpymongoclient=pymongo.mongo客户端(' MongoDB ://localhost 336027017/' 选择pythondb数据库数据库如何在pymongo模块中检索集合? 与获取数据库的方法相同。mongo _ URL=' localhost ' client=pymongo.mongo client (mongo _ URL ) pythondb数据库db=client [ ' pytyte ] 使用test集合的3.4插入文档数据必须在MongoDB中另存为JSON类文件,并且存储在MongoDB数据库中的数据类型必须为{key:value}类型。 在pymongo模块中使用inset_one方法插入文档,并将爬网的新书信息保存到MongoDB数据库中。 fromlxmlimportetreefromseleniumimportwebdriverdriver=web driver.Firefox () driver.get (' https://www.Sina.com.cn/' hml=driver.page _ source driver.close (mongo _ URL=' localhost ' client=pymongo.mongo client ) mongo_uurl ' print(html ) html=etree.html ) html ) a=html.XPath ) (/* [ @ id=' syn CAD _0' ]/ul/Li/a/text ) ) coll mongod-- dbpath./mongo-versor mongopipinstallpymongoshowdbsusepythondbshowcollectionsdb.test1.find (.pretty ) ) )

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。