【发布时间】:2019-01-24 03:00:13
【问题描述】:
我是 python 和网络爬虫世界的初学者,我习惯于使用动态 URL 制作爬虫,当我在 URL 本身中输入特定参数时,URI 会发生变化。
例如:维基百科。
(如果我输入一个名为“Stack Overflow”的搜索,我将有一个如下所示的 URI:https://en.wikipedia.org/wiki/Stack_Overflow)
目前我面临的挑战是开发一个网络抓取工具来收集来自this page 的数据。
"Texto/Termos a serem pesquisados" 字段对应一个搜索字段,但是当我输入搜索时,URL 保持不变,无法让我为我的研究获取正确的 HTML 代码。
我习惯于使用 BeautifulSoup 和 Requests 进行抓取,但在这种情况下它没有用,因为搜索后 URL 保持不变。
import requests
from bs4 import BeautifulSoup
url = 'http://comprasnet.gov.br/acesso.asp?url=/ConsultaLicitacoes/ConsLicitacao_texto.asp'
html = requests.get(url)
bs0bj = BeautifulSoup(html.content,'html.parser')
print(bsObj)
# And from now on i cant go any further
通常我会做类似的事情
url = 'https://en.wikipedia.org/wiki/'
input = input('Input your search :)
search = url + input
然后做所有 BeautifulSoup 的事情,然后 findAll 事情来从 HTML 代码中获取我的数据。
我也尝试过使用 Selenium,但由于所有 webdriver 的原因,我正在寻找与此不同的东西。使用以下代码,我取得了一些奇怪的结果,但我仍然无法很好地抓取 HTML。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import requests
from bs4 import BeautifulSoup
# Acess the page and input the search on the field
driver = webdriver.Chrome()
driver.get('http://comprasnet.gov.br/acesso.asp?url=/ConsultaLicitacoes/ConsLicitacao_texto.asp')
driver.switch_to.frame('main2')
busca = driver.find_element_by_id("txtTermo")
busca.send_keys("GESTAO DE PESSOAS")
#data_inicio = driver.find_element_by_id('dt_publ_ini')
#data_inicio.send_keys("01/01/2018")
#data_fim = driver.find_element_by_id('dt_publ_fim')
#data_fim.send_keys('20/12/2018')
botao = driver.find_element_by_id('ok')
botao.click()
考虑到所有这些:
- 有没有办法从这些静态 url 中抓取数据?
- 我可以通过代码在字段中输入搜索吗?
- 为什么我不能抓取正确的源代码?
【问题讨论】:
标签: python python-3.x web-scraping beautifulsoup python-requests