があるヘッダを理解しようと要求が適切に要求を模倣するために作られています。
また、リクエストの送信元を確認して、コンテンツが動的にロードされているか、レスポンス自体がロードされているかどうかを確認してください。
あなたはあなたが必要なものを達成するために、このような何かを行うことができます。これはになります
from lxml import html
import requests
payload = {
'chiavi[nome]' : 'a',
'chiavi[cognome]' : '',
'chiavi[cap]' : '',
'chiavi[niscrizione]' : '',
'chiavi[qualealbo]' : '-1',
'4d9541a6aa764e6927da818df9807154' : '1',
'task':'cerca',
'view':'iscritti'
}
headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'en-US,en;q=0.8,ms;q=0.6',
'Cache-Control':'no-cache',
'Connection':'keep-alive',
'Content-Length':'166',
'Content-Type':'application/x-www-form-urlencoded',
'Cookie':'eae5024ccd0dde80ea5c031989e5bbb1=9prqrhe6ufli980heiknmhe1b2',
'Host':'www.odcec.pescara.it',
'Origin':'http://www.odcec.pescara.it',
'Pragma':'no-cache',
'Referer':'http://www.odcec.pescara.it/index.php?option=com_wbmalbo&view=iscritti&Itemid=16',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
}
url = 'http://www.odcec.pescara.it/index.php?option=com_wbmalbo&view=iscritti&Itemid=16'
r = requests.post(url, data = payload, headers = headers)
root = html.fromstring(r.content)
names_list = [' '.join(names.xpath('.//p/text()')) for names in root.xpath('//td[@class="arm-col1"]/a')]
print(names_list)
:以下のページから電子メールを取得するには
['Agresta Adelchi', 'Agresta Alberto', 'Agresti Antonio Donato', 'Aielli Alfonso', 'Albieri Alessandro', 'Altamura Alessandra', 'Amicantonio Alessandro', 'Anchini Andrea', 'Andreoli Antonella', 'Andreucci Anna', 'Antonucci Alessia', 'Appignani Antonio', 'Arienti Alessandro', 'Ascione Alessandro', 'Barbone Alessandro', 'Barbone Andrea', 'Basso Angela', 'Bellino Alberto', 'Berardocco Alberto', 'Berghella Alessandra']
、あなたのような何かを行うことができますこれは、を覚えておいてください。のWebサイトにはリクエストがありません。また
['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']
、私はあなたが多くのページのためにそれを行う必要がある場合は、scrapyを検討することをお勧め:
from lxml import html
import requests
from time import sleep
payload = {
'chiavi[nome]' : 'a',
'chiavi[cognome]' : '',
'chiavi[cap]' : '',
'chiavi[niscrizione]' : '',
'chiavi[qualealbo]' : '-1',
'4d9541a6aa764e6927da818df9807154' : '1',
'task':'cerca',
'view':'iscritti'
}
headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'en-US,en;q=0.8,ms;q=0.6',
'Cache-Control':'no-cache',
'Connection':'keep-alive',
'Content-Length':'166',
'Content-Type':'application/x-www-form-urlencoded',
'Cookie':'eae5024ccd0dde80ea5c031989e5bbb1=9prqrhe6ufli980heiknmhe1b2',
'Host':'www.odcec.pescara.it',
'Origin':'http://www.odcec.pescara.it',
'Pragma':'no-cache',
'Referer':'http://www.odcec.pescara.it/index.php?option=com_wbmalbo&view=iscritti&Itemid=16',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
}
url = 'http://www.odcec.pescara.it/index.php?option=com_wbmalbo&view=iscritti&Itemid=16'
r = requests.post(url, data = payload, headers = headers)
root = html.fromstring(r.content)
names_list = [' '.join(names.xpath('.//@onclick')).replace('javascript:submitform(', '').replace(')', '') for names in root.xpath('//td[@class="arm-col1"]/a')]
email_list = []
for codes in names_list :
payload1 = {
'task' : 'schedaIscritto',
'view' : ' iscritti',
'id' : '25',
'4d9541a6aa764e6927da818df9807154' : '1'
}
r1 = requests.post(url, data = payload1, headers = headers)
root1 = html.fromstring(r1.content)
email_list.append(''.join(root1.xpath('//*[contains(text(),"E-mail")]/following-sibling::*/text()')))
sleep(5)
print email_list
これは、になります。
あなたのために仕事を完了するためにここにはないので、解決しようとしたコードを追加してください。 –
@SatishGarg試行されたコード –
を追加しました。投稿要求を出すのではなく、リクエストを受け取ります。ブラウザの[ネットワーク]タブを確認することができます。また、要求を複製するために必要な投稿データとヘッダーを追加します。 –