最近在爬一個網(wǎng)站,想直接下載其中的torrent文件,發(fā)現(xiàn)該torrent文件在下載頁面是點(diǎn)擊下載按鈕,提交一個form表單到后臺,然后開始下載,使用python requests提交表單,但是下載下來的文件是論壇的首頁,不知道哪里出錯了,有大佬幫看看嗎?以下是代碼,老司機(jī)懂的.
def down_torrent(link):
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
"Accept-Encoding": "gzip, deflate"
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8"
"Cache-Control": "max-age=0"
"Connection": "keep-alive"
"Host": "www3.uptorrentfilespacedownhostabc.info"
"Upgrade-Insecure-Requests": "1"
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.360"
}
torrent_html = requests.get(link)
torrent_html.encoding == 'utf-8'
soup = BeautifulSoup(torrent_html.text, 'lxml')
input_list = soup.find_all('input')
down_parms = {} #POST提交的參數(shù)
for i in input_list:
if i.get('name'):
down_parms[i.get('name')] = i.get('value')
#POST地址
post_link = link.split('file.php')[0] + 'down.php'
#下載文件保存位置
down_dir = os.path.join(current_path, down_parms['name'])
if not os.path.exists(down_dir):
os.mkdir(down_dir)
down_path = os.path.join(down_dir, down_parms['name'])
down_path = down_path + '.torrent'
s = requests.Session()
torrent_html = s.post(post_link, headers=headers, data=down_parms)
with open(down_path, 'wb') as f:
for chunk in torrent_html.iter_content(10):
f.write(chunk)
if __name__ == '__main__':
down_torrent('http://www3.uptorrentfilespacedownhostabc.info/updowm/file.php/P22OGZq.html')
恩,很黃很暴力,我很喜歡
import requests
from pyquery import PyQuery as Q
url = 'http://www3.uptorrentfilespacedownhostabc.info/updowm/file.php/P22OGZq.html'
post_url = 'http://www3.uptorrentfilespacedownhostabc.info/updowm/down.php'
session = requests.Session()
session.headers['Referer'] = url
r = session.get(url)
inputs = Q(r.text).find('input[type="hidden"]')
data = {Q(_).attr('name'): Q(_).attr('value') for _ in inputs}
r = session.post(post_url, data=data)
file_name = '{0}.torrent'.format(data['name'])
with open(file_name, 'wb') as f:
f.write(r.content)
我試了下,header里面必須有"Referer",否則會請求超時,但是并沒出現(xiàn)響應(yīng)是首頁的情況
import requests
def download():
url = 'http://www3.uptorrentfilespacedownhostabc.info/updowm/down.php'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit \
/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image \
/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN, zh; q=0.9',
'Referer': 'http://www3.uptorrentfilespacedownhostabc.info/updowm/file.php/P22OGZq.html',
# 'Origin': 'http://www3.uptorrentfilespacedownhostabc.info',
'Upgrade-Insecure-Requests': '1',
'Host': 'www3.uptorrentfilespacedownhostabc.info',
}
post_data = {'type': 'torrent',
'id': 'P22OGZq',
'name': '55d19496b8995be1f2c1118daa47ba4c4c8b98ec'}
response = requests.post(url, headers=headers, data=post_data)
if response.status_code == 200:
print(response.text)
else:
print(response.status_code)
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。