鱼C论坛

 找回密码
 立即注册
查看: 1153|回复: 3

[已解决]求助如何爬虫中遇到内置选择表格

[复制链接]
发表于 2022-3-21 20:27:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
最近学习了小甲鱼的效率革命的爬虫教程,但是在实际爬取网页时候还是遇到一些难以解决的问题想求助各位鱼油!!!
在爬取这个网址的时候,网址如下:

http://spf.szfcweb.com/szfcweb/(S(yhdau53nnmqkevmkjhamlddp))/DataSerach/MITShowList.aspx

发现 项目区域   可以选择不同的区域,如工业园区,吴中区等。选择不同的区域,下面表格的内容会对应到相应的区的项目,但网址的url并不会改变。 所以之前学到通过传递url的参数的方式好像也没有用了。想问问这种情况下如何爬取各个区的项目情况呢?请各位鱼油指教!
最佳答案
2022-3-23 02:15:11
  1. #!/usr/bin/env python3
  2. # _*_ coding: utf-8 _*_
  3. # Developer: suchocolate
  4. # Date: 2022-03-23 01:16
  5. # File name: pc1.py
  6. # Development tool: PyCharm

  7. import requests
  8. from lxml import etree


  9. def get_args(txt, f=0):
  10.     html = etree.HTML(txt)
  11.     sta = html.xpath('//input[@id="__VIEWSTATE"]/@value')[0]
  12.     gen = html.xpath('//input[@id="__VIEWSTATEGENERATOR"]/@value')[0]
  13.     eve = html.xpath('//input[@id="__EVENTVALIDATION"]/@value')[0]
  14.     if not f:
  15.         return sta, gen, eve
  16.     else:
  17.         zones = html.xpath('//select[@name="ctl00$MainContent$ddl_RD_CODE"]/option/@value')
  18.         return sta, gen, eve, zones


  19. def main():
  20.     headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0',
  21.                'Origin': 'http://spf.szfcweb.com',
  22.                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  23.                'Host': 'spf.szfcweb.com',
  24.                'Accept-Encoding': 'gzip, deflate',
  25.                'Content-Type': 'application/x-www-form-urlencoded',
  26.                'Upgrade-Insecure-Requests': '1',
  27.                'Referer': 'http://spf.szfcweb.com/szfcweb/(S(r2ga2fthdmz1mel1rym2xyat))/DataSerach/MITShowList.aspx'}
  28.     url = 'http://spf.szfcweb.com/szfcweb/(S(r2ga2fthdmz1mel1rym2xyat))/DataSerach/MITShowList.aspx'
  29.     r = requests.get(url, headers=headers)
  30.     sta, gen, eve, zones = get_args(r.text, f=1)
  31.     for zone in zones:
  32.         data = {'__EVENTTARGET': '',
  33.                 '__EVENTARGUMENT': '',
  34.                 '__LASTFOCUS': '',
  35.                 '__VIEWSTATE': sta,
  36.                 '__VIEWSTATEGENERATOR': gen,
  37.                 '__EVENTVALIDATION': eve,
  38.                 'ctl00$MainContent$txt_Com': '',
  39.                 'ctl00$MainContent$txt_Pro': '',
  40.                 'ctl00$MainContent$ddl_RD_CODE': zone,
  41.                 'ctl00$MainContent$txt_ysz': '',
  42.                 'ctl00$MainContent$bt_select': '查询',
  43.                 'ctl00$MainContent$PageGridView1$ctl12$PageList': '0'
  44.                 }
  45.         r = requests.post(url, headers=headers, data=data)
  46.         # print(r.status_code)
  47.         print(r.text)
  48.         sta, gen, eve = get_args(r.text)


  49. if __name__ == '__main__':
  50.     main()
复制代码
预售证.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-23 01:58:24 | 显示全部楼层
Screenshot 2022-03-23 015543.jpg
选不同的项目,它会带不同的参数发给网站。
这些参数来自上一次访问,所以你每次注意收集一下参数,并且带上去就能查询到了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-23 02:15:11 | 显示全部楼层    本楼为最佳答案   
  1. #!/usr/bin/env python3
  2. # _*_ coding: utf-8 _*_
  3. # Developer: suchocolate
  4. # Date: 2022-03-23 01:16
  5. # File name: pc1.py
  6. # Development tool: PyCharm

  7. import requests
  8. from lxml import etree


  9. def get_args(txt, f=0):
  10.     html = etree.HTML(txt)
  11.     sta = html.xpath('//input[@id="__VIEWSTATE"]/@value')[0]
  12.     gen = html.xpath('//input[@id="__VIEWSTATEGENERATOR"]/@value')[0]
  13.     eve = html.xpath('//input[@id="__EVENTVALIDATION"]/@value')[0]
  14.     if not f:
  15.         return sta, gen, eve
  16.     else:
  17.         zones = html.xpath('//select[@name="ctl00$MainContent$ddl_RD_CODE"]/option/@value')
  18.         return sta, gen, eve, zones


  19. def main():
  20.     headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0',
  21.                'Origin': 'http://spf.szfcweb.com',
  22.                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  23.                'Host': 'spf.szfcweb.com',
  24.                'Accept-Encoding': 'gzip, deflate',
  25.                'Content-Type': 'application/x-www-form-urlencoded',
  26.                'Upgrade-Insecure-Requests': '1',
  27.                'Referer': 'http://spf.szfcweb.com/szfcweb/(S(r2ga2fthdmz1mel1rym2xyat))/DataSerach/MITShowList.aspx'}
  28.     url = 'http://spf.szfcweb.com/szfcweb/(S(r2ga2fthdmz1mel1rym2xyat))/DataSerach/MITShowList.aspx'
  29.     r = requests.get(url, headers=headers)
  30.     sta, gen, eve, zones = get_args(r.text, f=1)
  31.     for zone in zones:
  32.         data = {'__EVENTTARGET': '',
  33.                 '__EVENTARGUMENT': '',
  34.                 '__LASTFOCUS': '',
  35.                 '__VIEWSTATE': sta,
  36.                 '__VIEWSTATEGENERATOR': gen,
  37.                 '__EVENTVALIDATION': eve,
  38.                 'ctl00$MainContent$txt_Com': '',
  39.                 'ctl00$MainContent$txt_Pro': '',
  40.                 'ctl00$MainContent$ddl_RD_CODE': zone,
  41.                 'ctl00$MainContent$txt_ysz': '',
  42.                 'ctl00$MainContent$bt_select': '查询',
  43.                 'ctl00$MainContent$PageGridView1$ctl12$PageList': '0'
  44.                 }
  45.         r = requests.post(url, headers=headers, data=data)
  46.         # print(r.status_code)
  47.         print(r.text)
  48.         sta, gen, eve = get_args(r.text)


  49. if __name__ == '__main__':
  50.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-5-5 07:57:59 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 00:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表