Learning python seventh day notes

37,055
modified headers
① parameter by modifying the Request headers
added by Request.add_header () method ②

Agent
1, create a proxy_support = urllib.request.ProxyHandler ({})

2, custom, create an opener
opener = urllib.request.builb_opener (proxy_support)

3、安装opener
urllib.request.install_opener(opener)

4、调用opener
opener.open(url)

For example:
Import urllib.request
Import Random

url = 'http: //www.whatismyip.com.tw'# can view the site its own IP address, can be another network.
= IPLIST [ '1.245.107.123:3128','203.246.112.133:3128','200.89.174.245:8080']
proxy_support = urllib.request.ProxyHandler ({ 'HTTP': The random.choice (IPLIST)})
opener = urllib.request.build_opener (proxy_support)
urllib.request.install_opener (opener)
Response = the urllib.request.urlopen (URL)
HTML response.read = (). decode ( 'UTF-. 8')

print(html)

38、055
隐藏
import urllib.request
import urllib.parse
import json

content = input ( "Please enter the content to be translated:")

url = 'http://fy.iciba.com/ajax.php?a=fy'
data={}
data['f'] = 'auto'
data['t'] = 'auto'
data['w'] = content
data = urllib.parse.urlencode(data).encode('utf-8')

req = urllib.request.Request(url,data)
#模拟网页登录
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36')

response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')

= json.loads targel (HTML)
Print ( "translation:% s"% targel [ ' content'] [ 'out'])

39、56

40,057
regular expressions need to import the re module, re e.g. Import
r.search (R & lt 'need to find the content', 'the content of the statement')
r.search (R & lt '[Find What]', 'look statement') brackets may create a character class, a letter or number so long as any matched to the brackets are considered to match the
number of times r.search (r'ab {3,10} c ' ,' abbbbbc ') braces are allowed to b three to ten times

41,058
^ meant to be the beginning.
$ Meaning in what the end. For example: r.search (r'abc $ ', 123.abc ) which may be matched to
* = {0, from zero to positive infinity}
+ = {1, n} from one to infinity
= {0,1}? from zero to a
# re.findall () can be matched to the content packed into a table.

42,059
the p-re.compile = ( "need to call the content")
p.search ( "query")
p.findall ( "query")
to support space under re.VERBOSE mode

Guess you like

Origin www.cnblogs.com/dcpb/p/11595070.html