Python 如何从aspx页面中刮取图像?
我正在尝试从一个aspx页面上刮取图像我有一段代码,可以从普通网页上刮取图像,但不能刮取aspx页面,因为我需要向aspx页面发送http post请求,即使读了几个线程,我也不知道该怎么做这是原始代码Python 如何从aspx页面中刮取图像?,python,html,asp.net,python-2.7,web-scraping,Python,Html,Asp.net,Python 2.7,Web Scraping,我正在尝试从一个aspx页面上刮取图像我有一段代码,可以从普通网页上刮取图像,但不能刮取aspx页面,因为我需要向aspx页面发送http post请求,即使读了几个线程,我也不知道该怎么做这是原始代码 from bs4 import BeautifulSoup as bs import urlparse import urllib2 from urllib import urlretrieve import os import sys import subprocess import re
from bs4 import BeautifulSoup as bs
import urlparse
import urllib2
from urllib import urlretrieve
import os
import sys
import subprocess
import re
def thefunc(url, out_folder):
c = False
我已经为aspx页面定义了标题,并定义了区分普通页面和aspx页面的if语句
aspx页面的标题
我对aspx检测和创建aspx页面的标题做了一些修改,但是如何修改下一步我被困在这里
***以下是aspx页面链接***
对不起,如果我不清楚,你可以看到我是编程新手,我要问的问题是,当我单击浏览器中的“下一页”按钮时,如何获取从aspx页面获取的图像?原因是如果我只能刮取一页,则url不会更改,除非我以某种方式发送http帖子,告诉页面显示下一页的新图片,因为url保持不变,我希望我清楚地知道,我的互联网在我所在的位置非常糟糕,所以我不能100%保证这一切正常,但你想要执行的是在这两行之间 这适用于任何类型的页面。如果我解释错了什么,不要保留评论
import urllib2
from urlparse import urljoin
from urllib import urlretrieve
from bs4 import BeautifulSoup
url = "http://www.foxrun.com.au/Products/Cylinders_with_Gadgets.aspx"
html = urllib2.urlopen(url)
soup = BeautifulSoup(html)
imgs = soup.findAll("img")
image=0
for img in imgs:
link=urljoin(url,img['src']) #Join relative paths
urlretrieve(link, "image"+str(image)) #saves image in the folder you execute this
image+=1 #increments name
这将产生
图1
图2
...
图像
根据需要更改目标路径
编辑:
这与aspx无关
页面链接是javascript生成的,因此无法从中提取url。urrlib不处理动态生成的内容,因此在这种情况下,您必须使用浏览器仿真器,比如+Firefox/或者您可以使用。还有+幻影。可能性是无穷的,但我会选择Selenium:
使用这些工具,您可以像在浏览器中一样与页面交互单击、滚动、向框中输入文本等您可以通过向url发布正确的数据来完成此操作,您可以从初始页面解析这些数据:
import requests
from bs4 import BeautifulSoup
from urlparse import urljoin
from itertools import chain
url = "http://www.foxrun.com.au/Products/Cylinders_with_Gadgets.aspx"
def validate(soup):
return {"__VIEWSTATE": soup.select_one("#__VIEWSTATE")["value"],
"__VIEWSTATEGENERATOR": soup.select_one("#__VIEWSTATEGENERATOR")["value"],
"__EVENTVALIDATION": soup.select_one("#__EVENTVALIDATION")["value"]}
def parse(base, url):
data = {"__ASYNCPOST": "true"
}
h = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17'}
soup = BeautifulSoup(requests.get(url).text)
data.update(validate(soup))
# gets links for < 1,2,3,4,5,6>
pages = [a["id"] for a in soup.select("a[id^=ctl01_ctl00_pbsc1_pbPagerBottom_btnP]")][2:]
# get images from initial page
yield [img["src"] for img in soup.select("img")]
# add token for post
data.update(validate(soup))
for p in pages:
# we need $ in place of _ for the form data
data["__EVENTTARGET"] = p.replace("_", "$")
data["RadScriptManager1"] = "ctl01$ctl00$pbsc1$ctl01$ctl00$pbsc1$ajaxPanel1Panel|{}".format(p.replace("_", "$"))
r = requests.post(url, data=data, headers=h).text
soup = BeautifulSoup(r)
yield [urljoin(base, img["src"]) for img in soup.select("img")]
for url in chain.from_iterable(parse("http://www.foxrun.com.au/", url)):
print(url)
这将为您提供链接,您只需下载内容并将其写入文件。通常我们可以创建一个会话并从一个页面转到下一个页面,但在这种情况下,发布的是ctl01$ctl00$pbsc1$PBPAGERBOTOM$btnNext,从第一个页面转到第二个页面很好,但没有从第二个页面转到第三个页面的概念,等等。。因为表单数据中没有页码。很抱歉,如果我不清楚,正如您所看到的,我是编程新手,我要问的问题是,当我单击浏览器中的“下一页”按钮时,如何获取从aspx页面获取的图像?原因是如果我只能刮取一页,则url不会更改,除非我以某种方式发送http帖子,告诉页面显示下一页的新图片,因为url保持不变,我希望我是clear@ShantanuBedajna更新,祝你好运,如果这个答案对你有帮助,请将其标记为正确:这就是我一直在寻找的方法,谢谢
import urllib2
from urlparse import urljoin
from urllib import urlretrieve
from bs4 import BeautifulSoup
url = "http://www.foxrun.com.au/Products/Cylinders_with_Gadgets.aspx"
html = urllib2.urlopen(url)
soup = BeautifulSoup(html)
imgs = soup.findAll("img")
image=0
for img in imgs:
link=urljoin(url,img['src']) #Join relative paths
urlretrieve(link, "image"+str(image)) #saves image in the folder you execute this
image+=1 #increments name
import requests
from bs4 import BeautifulSoup
from urlparse import urljoin
from itertools import chain
url = "http://www.foxrun.com.au/Products/Cylinders_with_Gadgets.aspx"
def validate(soup):
return {"__VIEWSTATE": soup.select_one("#__VIEWSTATE")["value"],
"__VIEWSTATEGENERATOR": soup.select_one("#__VIEWSTATEGENERATOR")["value"],
"__EVENTVALIDATION": soup.select_one("#__EVENTVALIDATION")["value"]}
def parse(base, url):
data = {"__ASYNCPOST": "true"
}
h = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17'}
soup = BeautifulSoup(requests.get(url).text)
data.update(validate(soup))
# gets links for < 1,2,3,4,5,6>
pages = [a["id"] for a in soup.select("a[id^=ctl01_ctl00_pbsc1_pbPagerBottom_btnP]")][2:]
# get images from initial page
yield [img["src"] for img in soup.select("img")]
# add token for post
data.update(validate(soup))
for p in pages:
# we need $ in place of _ for the form data
data["__EVENTTARGET"] = p.replace("_", "$")
data["RadScriptManager1"] = "ctl01$ctl00$pbsc1$ctl01$ctl00$pbsc1$ajaxPanel1Panel|{}".format(p.replace("_", "$"))
r = requests.post(url, data=data, headers=h).text
soup = BeautifulSoup(r)
yield [urljoin(base, img["src"]) for img in soup.select("img")]
for url in chain.from_iterable(parse("http://www.foxrun.com.au/", url)):
print(url)