本篇代码提供者: 青灯教育-巳月
所需第三方库:
- requests >>> pip install requests
- parsel >>> pip install parsel
开发环境:
- 版 本: python 3.8
- 编辑器: pycharm 2021.2
如果安装python第三方模块:
- win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车
- 在pycharm中点击Terminal(终端) 输入安装命令
如何配置pycharm里面的python解释器?
- 选择file(文件) >>> setting(设置) >>> Project(项目) >>> python interpreter(python解释器)
- 点击齿轮, 选择add
- 添加python安装路径
pycharm如何安装插件?
- 选择file(文件) >>> setting(设置) >>> Plugins(插件)
- 点击 Marketplace 输入想要安装的插件名字 比如:翻译插件 输入 translation / 汉化插件 输入 Chinese
- 选择相应的插件点击 install(安装) 即可
- 安装成功之后 是会弹出 重启pycharm的选项 点击确定, 重启即可生效
代码
因审核机制原因,我把网址里的一些东西删掉了,小可耐们可以自己添加一下哈,很容易的
如果有不太会改或者有点点小懒惰的小可耐也可以私信我,我发你呐~ ?
一、导入模块
# Python里面有非常多的第三方工具
# 内置工具也有很多
import requests # 发送请求 第三方模块
import parsel # 解析数据 第三方模块
- 1
- 2
- 3
- 4
二、加入伪装
# 伪装 Python 字典容器
headers = {
# 用户信息
'cookie': 'TAUnique=%1%enc%3AHARC1EMLan58P07MI4ZMcqI%2BzHGWuLGBt6TE6zQDNwk%3D; TASSK=enc%3AAL%2Bm9xwFy7%2BjYONIRS%2F2kEbA%2FtOrlDbcW%2FwCSHs44XP9R3ddE%2BKJxi3FiDuozLe0Ov2ujtnFah8i0sN%2FRdUxZGis0TClwsaz7%2B7Uv8dh%2BvHM%2FfH9C%2FcEYLBYBtn1yLmBNg%3D%3D; _lc2_fpi=b140173de591--01fxvvhm5q52dte42gshbn1234; __gads=ID=887c76ae8964a5bc:T=1646981079:S=ALNI_MYwTZNsJPdidCGF3BTM3pOV79wAUg; TADCID=1dW7BfvYitWbqfH6ABQCFdpBzzOuRA-9xvCxaMyI1247i3OJBq9qNVWvXr3O78hI1lbd1kQngmuzyADlwihJyk7Y0wRjaf58AtM; PAC=AN2PxgmiZ1GRIiU_4c0KpV6mA_btSrKwqVybwoo_d5lgw6zXj_ZRZjSJBwH84o9UVTmeAgNhfd51gzwBlQcgcKT1VFPbzjf_j4wqU-2ufAxdlELGL3F1zacCWByaKOXRvixlLn4OXM42wYZ_VBHqSpVYj7ST_l8sbYSeYnXyqk0D1fTKvJaddWGzHbBAlKHZ2H5I_u-nk6oDQ9BooTrGu-lfGWRQfA0yVhJ1jXpar4dvw3Jw2M0hgdP0MnX1GhSkt_7ddkbOm4oPgbBlDwQ4iJrY6ZtWR0F1dQ_BvzH6z0ppsUXSC-KMe61YA3ZX0ZMJ9p2s3mGMo3ecdJCt44MxAQi_7Jqzg4myLdYV6kjlaix1; ServerPool=B; PMC=V2*MS.22*MD.20220616*LD.20220617; TASID=023207A6A44049DBBF0ACA838F764F72; ak_bmsc=894F53FCE8B8FB7782AE15FEFA7E8955~000000000000000000000000000000~YAAQyDUduAriZHGBAQAA5Sx5cRC2rV78bFA3eNvz3z/yLfcD9nJy7/TnvzWHLO79a8SrgTWtxV4unPZ46gsMj5v6GnffWnmSpowL7uO42YRsiEcF3kTsZFrGO1eIeTwldN6IfHI3XuhYEuvo9p1Q+kP/9+Ky1rGJHCadqhpYxM5ahkqaLr9Aq4SM93e5IAqEMVNbOIZktbURDBpQaykoFKmvNjEq2CgXidvUVXtBzRXweSMucJUCF10Gis1bysoJDGCPz3GHXE+j4yb4BeaeDugpLZ8lv5976By7wSGOtmsig1VDT5PXGIK7U625rni04ZtBupRZaEWjpTEaIT75jEszZlCD0XmxWulM9hV++OjMxaubsZh7Z3fnJz11fDy+YrItlxOk/KUbzK6UxA==; TATravelInfo=V2*A.2*MG.-1*HP.2*FL.3*RS.1*RY.2022*RM.6*RD.17*RH.20*RG.2; TAReturnTo=%1%%2FRestaurant_Review-g188590-d13511379-Reviews-Restaurant_Bougainville-Amsterdam_North_Holland_Province.html; OptanonConsent=isGpcEnabled=0&datestamp=Fri+Jun+17+2022+20%3A11%3A08+GMT%2B0800+(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)&version=6.30.0&isIABGlobal=false&hosts=&consentId=cc7e2f72-5007-428f-a72e-392f9741b69d&interactionCount=1&landingPath=NotLandingPage&groups=C0001%3A1%2CC0002%3A1%2CC0003%3A1%2CC0004%3A1&AwaitingReconsent=false; TATrkConsent=eyJvdXQiOiIiLCJpbiI6IkFMTCJ9; _pbjs_userid_consent_data=3524755945110770; _li_dcdm_c=.tripadvisor.com; __gpi=UID=000006b77138250b:T=1655467873:RT=1655467873:S=ALNI_MbOL4NX_tligitacN_KwB6PemYWqQ; SRT=TART_SYNC; TART=%1%enc%3AfD9OzCOGTHLKxR1qLNfmGZurd9xliidHT5bmQw2z505WnDQeBJdPDWc64WFlxikpNox8JbUSTxk%3D; _lr_sampling_rate=100; _lr_retry_request=true; _lr_env_src_ats=false; __li_idex_cache=%7B%7D; pbjs_li_nonid=%7B%7D; bm_sv=3B18902AF4DC3FB5C636585CAC8FF658~YAAQyDUduD5EZnGBAQAA2QmZcRDjjMYTHv4xqU4lD0E4d7Xj0WV5dQ/46jlfYTucTGl1yWVUXsAP3PcgXzxtrrNWkcjCugvrDcZNXH0mxKOlYlKNM3jWWQOOSt+Jen/ideuJLQAMpPBgXrEMW2v1qE1sM/09r8Wk1Jgb26a+Q9sao40/k/ESyx4ebtpyRkziMRByxOYBbirkTsVL/y7CYMPE8yVQqbiPQdHPem6GMrv+bp87L4gY9qqm7Z+g5eYWzimgOMkkWA==~1; __vt=SVdeZUe-0PX0kT-WABQCIf6-ytF7QiW7ovfhqc-AvR4ZUmM-8Xj_P7I6kBFYa-jE0Q5YcrsWTyJBGYS785HCCtk1Q1VlPRKksZAdqcsat21B56Go8VSeHHLIrlMmQ_OXo1YDVqVWeeFaKIh9pT3zHxOIvQ; roybatty=TNI1625!AF5lFidAXBie9fDE6jtKPZJbwpAH%2BdfYfemV%2FITVap8g312LO2OHEcRltEyEH1LU3KJ3IEadn4T4WB3lp%2BUqrWejbt9xHvyqboOAsCMoy1AI0wp2xItYlmihjGJ3Je%2FFAvbl9HW9bauuGGpnXM6W82PUibGqAIeVfxEwWnB2hFTH%2C1; TASession=V2ID.023207A6A44049DBBF0ACA838F764F72*SQ.30*LS.DemandLoadAjax*GR.9*TCPAR.43*TBR.37*EXEX.33*ABTR.12*PHTB.75*FS.38*CPU.34*HS.recommended*ES.popularity*DS.5*SAS.popularity*FPS.oldFirst*LF.en*FA.1*DF.0*TRA.false*LD.13511379*EAU._; TAUD=LA-1655364516989-1*RD-104046361-2022_06_17.13511379*LG-104047238-2.1.F.*LD-104047239-.....',
# 防盗链
'referer': '',
# 浏览器基本信息
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
}
url_ = ''
list_html = requests.get(url=url_, headers=headers).text
list_select = parsel.Selector(list_html)
link_list = list_select.css('.bGnIM .OhCyu span a::attr(href)').getall()
for link in link_list:
url = '' + link
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
三、发送请求
response = requests.get(url=url, headers=headers)
- 1
四、获取数据
html_data = response.text
- 1
五、解析数据
# 减轻负担
# 面试题 考试内容
selector = parsel.Selector(html_data)
store_name = selector.css('.fHibz::text').get()
comment_count = selector.css('.eSAOV.H3:nth-child(2) .eBTWs::text').get()
address = selector.css('.eSAOV.H3:nth-child(3) .dyeJW.dUpPX:nth-child(1) a::text').get()
# :nth-child(3): 选择第几个标签 采集多页内容
源码、解答、资料可加Q裙:832157862免费领取
phone = selector.css('.eSAOV.H3:nth-child(3) .dyeJW.dUpPX:nth-child(2) a::text').get()
score = selector.css('.eEwDq .fdsdx::text').get()
print(store_name, comment_count, address, phone, score)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
尾语
好了,我的这篇文章写到这里就结束啦!
有更多建议或问题可以评论区或私信我哦!一起加油努力叭(ง •_•)ง
喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!